<?php
/**
 * Created by PhpStorm.
 * User: 25791
 * Date: 2020/9/22
 * Time: 9:40
 * Note: stats.php
 */

namespace cli\crond\service;


use app\lib\base;

class stats extends base
{
    public int $day;

    /**
     * 日活
     *
     * @return int
     */
    public function live()
    {
        $key = "stats:lively:" . $this->day;
        return (int)$this->redis->sCard($key);
    }

    /**
     * 平均启动
     *
     * @return false|float|int
     */
    public function boot_avg()
    {
        $key1  = 'stats:operation:users:1:' . $this->day;
        $key2  = 'stats:operation:times:' . $this->day;
        $users = $this->redis->sCard($key1);
        $times = $this->redis->hGet($key2, 1);
        if (!$users || !$times) {
            return 0;
        }
        return round($times / $users, 2);
    }

    /**
     * 总分享用户数
     *
     * @return int
     */
    public function share_users()
    {
        $key1  = 'stats:operation:users:2:' . $this->day;
        $key2  = 'stats:operation:users:3:' . $this->day;
        $key3  = 'stats:operation:users:4:' . $this->day;
        $users = $this->redis->sUnion($key1, $key2, $key3);
        return count($users);
    }

    /**
     * 总分享次数
     *
     * @return int
     */
    public function share_times()
    {
        $key     = 'stats:operation:times:' . $this->day;
        $times_2 = (int)$this->redis->hGet($key, 2);
        $times_3 = (int)$this->redis->hGet($key, 3);
        $times_4 = (int)$this->redis->hGet($key, 4);
        return $times_2 + $times_3 + $times_4;
    }

    /**
     * 档案分享次数
     *
     * @return int
     */
    public function share_times_app()
    {
        $key = 'stats:operation:times:' . $this->day;
        return (int)$this->redis->hGet($key, 3);
    }

    /**
     * 资讯分享次数
     *
     * @return int
     */
    public function share_times_news()
    {
        $key = 'stats:operation:times:' . $this->day;
        return (int)$this->redis->hGet($key, 2);
    }

    /**
     * 首页分享次数
     *
     * @return int
     */
    public function share_times_home()
    {
        $key = 'stats:operation:times:' . $this->day;
        return (int)$this->redis->hGet($key, 4);
    }


    /**
     * 日活
     *
     * @return int
     */
    public function live_wx()
    {
        $key = "stats:lively_wx:" . $this->day;
        return (int)$this->redis->sCard($key);
    }

    /**
     * 总分享用户数
     *
     * @return int
     */
    public function share_users_wx()
    {
        $key1  = 'stats:operation_wx:users:2:' . $this->day;
        $key2  = 'stats:operation_wx:users:3:' . $this->day;
        $key3  = 'stats:operation_wx:users:4:' . $this->day;
        $users = $this->redis->sUnion($key1, $key2, $key3);
        return count($users);
    }

    /**
     * 总分享次数
     *
     * @return int
     */
    public function share_times_wx()
    {
        $key     = 'stats:operation_wx:times:' . $this->day;
        $times_2 = (int)$this->redis->hGet($key, 2);
        $times_3 = (int)$this->redis->hGet($key, 3);
        $times_4 = (int)$this->redis->hGet($key, 4);
        return $times_2 + $times_3 + $times_4;
    }

    /**
     * 档案分享次数
     *
     * @return int
     */
    public function share_times_app_wx()
    {
        $key = 'stats:operation_wx:times:' . $this->day;
        return (int)$this->redis->hGet($key, 3);
    }

    /**
     * 资讯分享次数
     *
     * @return int
     */
    public function share_times_news_wx()
    {
        $key = 'stats:operation_wx:times:' . $this->day;
        return (int)$this->redis->hGet($key, 2);
    }

    /**
     * 首页分享次数
     *
     * @return int
     */
    public function share_times_home_wx()
    {
        $key = 'stats:operation_wx:times:' . $this->day;
        return (int)$this->redis->hGet($key, 4);
    }
}